package vn.fithou.hou_elearning.activity;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import vn.fithou.hou_elearning.R;
import vn.fithou.hou_elearning.adapter.BaihocAdapter;
import vn.fithou.hou_elearning.adapter.KhoahocAdapter;
import vn.fithou.hou_elearning.parser.XMLParser;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class DanhsachBaihocActivity extends Activity {
	// All static variables
	static final String URL = "http://10.0.2.2/hou-elearning/Service/HOULMSService.svc/BaihocGetByFK_iKhoahocIDXML/";
	// XML node keys
	public static final String KEY_BAIHOC = "BaihocEntity"; // nút cha
	public static final String KEY_ID = "PK_iBaihocID";
	public static final String KEY_NAME = "sTenbaihoc";
	public static final String KEY_TITLE = "sThongtinbaihoc";
	public static final String KEY_THUMB_URL = "sAnhminhhoa";
	
	public static final int REQUEST_CODE_SLIDE = 2;
	
	ListView list;
    BaihocAdapter adapter;
    ArrayList<HashMap<String, String>> lessonList = null; //Danh sách bài học
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_layout);

		lessonList = new ArrayList<HashMap<String, String>>();

		//Get FK_iKhoahocID from DanhsachKhoahocActivity
		int FK_iKhoahocID = 0;
		Bundle extras = getIntent().getExtras();
		if(extras !=null) 
		{
			FK_iKhoahocID = extras.getInt("FK_iKhoahocID", 0);
		}
		XMLParser parser = new XMLParser();
		String xml = parser.getXmlFromUrl(URL + FK_iKhoahocID, "POST"); // getting XML from URL
		Document doc = parser.getDomElement(xml); // getting DOM element
		
		NodeList nl = doc.getElementsByTagName(KEY_BAIHOC);
		// looping through all song nodes <song>
		for (int i = 0; i < nl.getLength(); i++) {
			// creating new HashMap
			HashMap<String, String> map = new HashMap<String, String>();
			Element e = (Element) nl.item(i);
			// adding each child node to HashMap key => value
			map.put(KEY_ID, parser.getValue(e, KEY_ID));
			map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
			map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
			map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

			// adding HashList to ArrayList
			lessonList.add(map);
		}
		

		list = (ListView)findViewById(R.id.list);
		
		// Getting adapter by passing xml data ArrayList
        adapter = new BaihocAdapter(this, lessonList);        
        list.setAdapter(adapter); //Đặt adapter của list view
        

        /*
		 * Khi click vào khóa học -> lấy ID -> chuyển sang hiển thị danh sách bài học tương ứng
		 * */
        list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				int FK_iBaihocID = Integer.parseInt(lessonList.get(position).get(KEY_ID));
				hienthiDanhsachSlide(FK_iBaihocID);
			}
		});	
	}
	/*
	 * Hiển thị danh sách slide theo ID
	 * */
	public void hienthiDanhsachSlide(int FK_iBaihocID)
    {
        Intent inDsSlide = new Intent(DanhsachBaihocActivity.this, DanhsachSlideActivity.class);
        Bundle b = new Bundle();
        b.putInt("FK_iBaihocID", FK_iBaihocID);
        inDsSlide.putExtras(b);
        startActivityForResult(inDsSlide, REQUEST_CODE_SLIDE);
    }
}
